<?php
namespace Index\Controller;
use Think\Controller;
class CommonController extends Controller {
    
    public function _initialize()
    {

    	$this->getCategorys();
    	// 组合访问路径
    	$url = MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME;
    	// 判断权限
    	if(in_array($url, C('AUTH_INDEX'))){
    		if(!isset($_SESSION['username'])){
	    		$this->error('你还没有登录，请先登录',U('index/login/index'));
	    	}
    	}
    }

    public function getCategorys()
    {
    	// 判断缓存是否存在
    	if(S('categorys')){

    		$this->assign('categorys',S('categorys'));

    	}else{
    		// 查询数据，获取分类

    		$sql = "select * from category";
	    	$categorys = M()->query($sql);

	    	S('categorys',$categorys,60);

	    	$this->assign('categorys',$categorys);

    	}
    	
    }

    public function autoadd($table,$filed,$id)
    {
        $sql = "update {$table} set {$filed}={$filed}+1 where id='{$id}'";

        M()->execute($sql);

    }

}

